/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:StringUtil.java
 * Version:1.0
 * Date:2011-11-19
 * Description: 
 * author:zhijian.zhang
 */
package com.tietiao.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 * 
	 * 功能：判断给定的字符是不为空（字符为空、空串、null值时统一返回false）
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-5-27
	 * 
	 * @param str
	 *            需要判定的字符
	 * @return
	 */
	public static boolean checkStrIsNotNull(String str) {
		if (null == str || str.trim().length() == 0) {
			return false;
		}
		return true;
	}

	public static boolean strIsNull(String str) {
		return !checkStrIsNotNull(str);
	}

	/**
	 * 邮箱格式判断
	 * 
	 * @param str
	 * @return
	 */
	public static final boolean isEmailAdress(String str) {
		if (strIsNull(str)) {
			return false;
		} else {
			Pattern p = Pattern
					.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
			Matcher m = p.matcher(str);
			boolean b = m.matches();
			return b;
		}
	}

	/**
	 * 
	 * 功能:改造给定的字符，以符合mysql中not in 或 in 中需要接受的多个字符串条件 如str为"常用,备用" 改造后应该为
	 * “'常用','备用'”
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-20
	 * 
	 * @param str
	 *            需要改造的字符
	 * @return
	 */
	public static String wrapSqlInCondition(String str) {
		if (strIsNull(str)) {
			return null;
		}
		String[] tempArr = str.split(",");
		StringBuilder result = new StringBuilder();
		for (int i = 0, len = tempArr.length; i < len; i++) {
			String string = tempArr[i];
			result.append("'").append(string).append("'");
			if (i < len - 1) {
				result.append(",");
			}
		}
		return result.toString();
	}

	/**
	 * 
	 * 功能：将数组封装成SQL中IN条件的字符
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2012-1-2
	 * 
	 * @param str
	 * @param danyinFlag
	 *            是否带单引号
	 * @return
	 */
	public static String wrapSqlInCondition(String[] str, boolean danyinFlag) {
		if (str == null || str.length == 0) {
			return "";
		}
		StringBuilder inSql = new StringBuilder();
		for (int i = 0, len = str.length; i < len; i++) {
			String string = str[i];
			if (danyinFlag) {
				inSql.append("'").append(string).append("'");
			} else {
				inSql.append(string);
			}
			if (i < len - 1) {
				inSql.append(",");
			}
		}
		return inSql.toString();
	}

	/**
	 * 
	 * 功能：将排序字段与排序方式组合成SQL语句的排序类型 例如：orderName为USER_ID orderType为ASC 则返回结果为
	 * USER_ID ASC
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @param orderName
	 *            排序字段名称
	 * @param orderType
	 *            排序方式，默认为ASC
	 * @return
	 */
	public static String wrapSort(String orderName, String orderType) {
		if (StringUtil.strIsNull(orderName)) {
			return null;
		}
		String defaultOrderType = "ASC";
		if (StringUtil.checkStrIsNotNull(orderType)) {
			defaultOrderType = orderType;
		}
		return orderName + " " + defaultOrderType;
	}
	
	/**
	 * 
	 * 功能：将字符串转码
	 * @author: TT_LOVE
	 * @Date: 2012-1-6
	 *
	 * @param code
	 * @return
	 */
	public static String decode(String code){
		if (code != null){
			try {
				code = URLDecoder.decode(code, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return code;
	}
	
	/**
	 * 
	 * 功能：处理cookies 将中文转码
	 * @return
	 */
	public static String encode(String str){
		if(str==null){
			return null;
		}else{
			try {
				return URLEncoder.encode(str, "UTF-8");
			} catch (UnsupportedEncodingException e) {
			}
			return "";
		}
		
		
	}
	/**
	 * 在用GET方式传递参数时需要转换字符集
	 * @param originalString
	 * @return
	 */
	public static String ISO88591_TO_UTF8(String originalString){
		String rtnStr = "";
		try {
			rtnStr =(new String((originalString).getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
		return rtnStr;
	}
	
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(decode(URLEncoder.encode("测试", "UTF-8")));
	}
}